package com.panfeng.xcloud.boss.provider.member.security.auth;

import com.panfeng.xcloud.boss.provider.member.dao.mapper.UserBaseMapperExt;
import com.panfeng.xcloud.common.core.utils.MD5Utils;
import com.panfeng.xcloud.common.core.utils.StringUtils;
import com.panfeng.xcloud.common.security.vo.ExtGrantedAuthority;
import com.panfeng.xcloud.common.security.vo.SysSecurityUser;
import com.panfeng.xcloud.dao.member.entity.UserBase;
import com.panfeng.xcloud.dao.member.entity.UserPwd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 加载app用户信息详情service
 *
 * @author xiaobo
 * @version 1.0
 * @since 2018-12-28
 */
@Component
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserBaseMapperExt userBaseMapperExt;

	@Override
	public UserDetails loadUserByUsername(String username) {
		//验证码模式：username----->grantType + "_" + mobile + "_" + verifyCode +"_"+ clientId +"_" + clientSecret
		//密码模式：username----->grantType + "_" + accountNumber + "_" + password +"_"+ clientId +"_" + clientSecret
		//刷新token模式：username----->手机号或者邮箱
		if(username.indexOf("_") != -1){
			String [] userAttachData = username.split("\\_",5);
			UserBase userBase = userBaseMapperExt.selectByLoginAccount(userAttachData[1]);
			if (userBase != null) {
				List<ExtGrantedAuthority> authorityList = loadUserAuthorities(userBase.getUserId());
				log.info(">>> 查询当前用户信息loadUserByUsername，userId={},nickName={}<<<", userBase.getUserId(),userBase.getNickname());
				String grantType = userAttachData[0];
				String encryptPwd = "";
				if(StringUtils.isNotEmpty(grantType) && grantType.equals("mobile")){
					encryptPwd = MD5Utils.digest(userAttachData[2]);
					//验证码模式的话，username只可能是手机号
					return new SysSecurityUser(userBase.getUserId(), userBase.getMobile(), userBase.getNickname(),encryptPwd, authorityList);
				}else if(StringUtils.isNotEmpty(grantType) && grantType.equals("password")){
					String accountNumber = userAttachData[1];
					UserPwd userPwd = userBaseMapperExt.selectLoginPwdByAccount(accountNumber);
					encryptPwd = userPwd.getPwd();
					//密码模式的话，username可能是邮箱，也有可能是手机号
					return new SysSecurityUser(userBase.getUserId(), accountNumber, userBase.getNickname(),encryptPwd, authorityList);
				}
				//默认以用户手机号为username
				return new SysSecurityUser(userBase.getUserId(), userBase.getMobile(), userBase.getNickname(),encryptPwd, authorityList);
			} else {
				throw new UsernameNotFoundException("用户不存在");
			}
		}else{
			UserBase userBase = userBaseMapperExt.selectByLoginAccount(username);
			if(null != userBase){
				List<ExtGrantedAuthority> authorityList = loadUserAuthorities(userBase.getUserId());
				log.info(">>> 查询当前用户信息loadUserByUsername，userId={},nickName={}<<<", userBase.getUserId(),userBase.getNickname());
				UserPwd userPwd = userBaseMapperExt.selectLoginPwdByAccount(username);
				return new SysSecurityUser(userBase.getUserId(), userBase.getMobile(), userBase.getNickname(),userPwd.getPwd(), authorityList);
			}else{
				throw new UsernameNotFoundException("用户不存在");
			}
		}
	}

	public List<ExtGrantedAuthority> loadUserAuthorities(String userId){
		log.info(">>> 开始加载用户权限信息，userId={} <<<", userId);
		List<ExtGrantedAuthority> authorityList = new ArrayList<>();
		authorityList.add(new ExtGrantedAuthority("USER_ROLE"));
		return authorityList;
	}
}
